import { getUserChannels, getAllChannels } from '@/api/channels'

export default {
  namespaced: true,
  state: {
    // 我的频道数据
    channelList: [],
    allchannelList: []
  },
  mutations: {
    // 修改频道数据
    setChennelList(state, payload) {
      state.channelList = payload
    },
    // 添加频道
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    // 删除频道
    delChannelList(state, payload) {
      state.channelList = state.channelList.filter(
        (item) => item.id !== payload.id
      )
    },
    setAllChennelList(state, payload) {
      state.allchannelList = payload
    }
  },
  actions: {
    // 异步发送请求
    async setChennelList_Action(context) {
      try {
        const res = await getUserChannels()
        console.log(res)
        context.commit('setChennelList', res.data.channels)
      } catch (e) {
        console.log(e)
      }
    },
    async setAllChennelList_Action(context) {
      try {
        const res = await getAllChannels()
        console.log(res)
        context.commit('setAllChennelList', res.data.channels)
      } catch (e) {
        console.log(e)
      }
    }
  },
  getters: {
    optionChangeList(state) {
      // eslint-disable-next-line array-callback-return
      return state.allchannelList.filter((item) => {
        const res = state.channelList.some((channel) => {
          return channel.id === item.id
        })
        // console.log(res)
        if (res) {
          return false
        } else {
          return true
        }
      })
    }
  }
}
